//
//  ZCMeFooterView.m
//  ZCBaiSi
//
//  Created by zc on 16/7/18.
//  Copyright © 2016年 zc. All rights reserved.
//

#import "ZCMeFooterView.h"
#import "ZCHttpSessionManager.h"
#import "ZCMeSquare.h"
#import <MJExtension.h>
#import "ZCWebViewController.h"
#import "ZCMeSquareButton.h"
#import <SafariServices/SafariServices.h>
@interface ZCMeFooterView()
//@property (nonatomic, strong) NSMutableDictionary <NSString *,ZCMeSquare *> *allSquares;
@end
@implementation ZCMeFooterView

//- (NSMutableDictionary<NSString *,ZCMeSquare *> *)allSquares{
//    if (!_allSquares) {
//        self.allSquares = [NSMutableDictionary dictionary];
//    }
//    return _allSquares;
//}

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame: frame]) {
        //参数
        NSDictionary *params = @{@"a":@"square",@"c":@"topic"};
        
        [[ZCHttpSessionManager manager]GET:ZCCommonURL parameters:params progress:^(NSProgress * _Nonnull downloadProgress) {
           // NSLog(@"-----%@",downloadProgress);
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            //字典数组转化为模型数组
           NSArray *squares = [ZCMeSquare mj_objectArrayWithKeyValuesArray:responseObject[@"square_list"]];
            [self createSquarws:squares];
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            ZCLog(@"请求失败 - %@",error);
        }];
    }
    return self;
}

//根据模型数据创建对应的数组
- (void)createSquarws:(NSArray *)squares{
    NSUInteger count = squares.count;
    //设置按钮的尺寸
   // NSUInteger rowsCount = (总数 + 每一行最多显示的数量 - 1) / 每一行最多显示的数量；
    int maxColsCount = 4;//一行最多有4个
    CGFloat btnW = self.zc_width / maxColsCount ;
    CGFloat btnH = btnW;
    
    for (NSInteger i = 0; i < count; i++) {
       
        //创建按钮
        ZCMeSquareButton *btn = [ZCMeSquareButton buttonWithType:UIButtonTypeCustom];
        [btn addTarget:self action:@selector(buttnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:btn];
        //设置frame
        btn.zc_x = (i % maxColsCount) * btnW;
        btn.zc_y = (i / maxColsCount) *btnH;
        btn.zc_width = btnW - 1;
        btn.zc_height = btnH - 1;
        //位置对应的模型数据
        btn.square = squares[i];
        
        
      //  [btn setImage:[UIImage imageNamed:@"setup-head-default"] forState:UIControlStateNormal];
//        [[SDWebImageManager sharedManager] downloadImageWithURL:[NSURL URLWithString:square.icon] options:0 progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
//            [btn setImage:image forState:UIControlStateNormal];
//        }];
      //  self.allSquares[btn.currentTitle] = square;
    }
    //设置footer的高度 == 最后一个按钮的bottom
    self.zc_height = self.subviews.lastObject.zc_bottom;
    
    UITableView *tableView = (UITableView *)self.superview;
    tableView.tableFooterView = self;
    [tableView reloadData];//重新刷新数据（也会重新计算constantsize）
   // tableView.contentSize = CGSizeMake(0, self.zc_bottom); 不靠谱
}

- (void)buttnClick:(ZCMeSquareButton *)sender{
    ZCMeSquare  *square = sender.square;
    if ([square.url hasPrefix:@"http"]) {//hasprefix 以什么开头 利用webView加载URL即可
        ZCWebViewController *webView = [[ZCWebViewController alloc]init];
        webView.url =square.url;
        webView.navigationItem.title = square.name;
        //获得“我”模块对应的导航控制器
        UITabBarController *tabBarCon = (UITabBarController *)self.window.rootViewController;
        UINavigationController *nav = tabBarCon.selectedViewController;//选中的控制器
        [nav pushViewController:webView animated:YES];
    }else if ([square.url hasPrefix:@"mod"]){
        if ([square.url hasSuffix:@"BDJ_To_cHECK"]) {//hasSuffix 以什么结尾 rangOfString 判断字符串的范围和位置 它的location为 NSNotFound 什么也没有找到
            ZCLog(@"审帖");
        }
    }else{
        ZCLog(@"什么也不是");
    }
}

@end
